//
//  LGConsultRecordItemCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/10/18.
//  咨询记录 - 按钮选择

#import "LGConsultRecordItemCell.h"

@interface LGConsultRecordItemCell()<UITextFieldDelegate>
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UIButton *titleBtn;
/**   */
@property (nonatomic , strong)UIView *itemView;
/**   */
@property (nonatomic , strong)UITextField *contentTF;
/**   */
@property (nonatomic , strong)NSMutableArray *btnArray;

@end

@implementation LGConsultRecordItemCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

-(void)setType:(NSString *)type{
    _type = type;
    CGFloat marginTop = [type isEqualToString:@"radio"]?0:viewPix(12);
    CGFloat textHeight = [type isEqualToString:@"radio"]?0:viewPix(32);
    [self.contentTF mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.itemView.mas_bottom).offset(marginTop);
        make.height.equalTo(@(textHeight));
    }];
}

-(void)setTitle:(NSString *)title{
    _title = title;
    [self.titleBtn setTitle:title forState:UIControlStateNormal];
}

-(void)setResultArray:(NSArray *)resultArray{
    _resultArray = resultArray;
}

-(void)setItemArray:(NSArray *)itemArray{
    _itemArray = itemArray;
    for (UIButton *button in self.btnArray) {
        [button removeFromSuperview];
    }
    [self.btnArray removeAllObjects];
    CGFloat top = viewPix(6);
    CGFloat left = 0;
    CGFloat height = 34;
    CGFloat totalHeight = 0;
    CGFloat margin_H = viewPix(12);
    CGFloat margin_V = viewPix(12);
    CGFloat maxWidth = self.itemView.bounds.size.width;
    NSMutableArray *titleArray = [NSMutableArray array];
    for (NSInteger i=0; i<itemArray.count; i++) {
        NSString *title = LGNSString(itemArray[i][@"title"]);
        NSString *value = LGNSString(itemArray[i][@"value"]);
        [titleArray addObject:title];
        CGFloat textWidth = [title textWidthWithFont:LGFontWeight(15, UIFontWeightMedium) maxHeight:MAXFLOAT]+40;
        top = ((left+textWidth)>=maxWidth)?(top+height+margin_V):top;
        left = ((left+textWidth)>=maxWidth)?0:left;
        UIButton *button = [UIButton buttonWithTitle:title titleFont:LGFontWeight(15, UIFontWeightMedium) normalColor:@"#666666" selectColor:@"#FFFFFF"];
        button.frame = CGRectMake(left, top, textWidth, height);
        [button setBackgroundColor:[UIColor colorWithString:@"#F6F6F6"] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor colorWithString:@"#3291FF"] forState:UIControlStateSelected];
        button.selected = [self.resultArray containsObject:value]?YES:NO;
        button.cornerRidus = 17;
        button.tag = 1000+i;
        [button addTarget:self action:@selector(itemButtonTouchAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.itemView addSubview:button];
        [self.btnArray addObject:button];
        left += textWidth+margin_H;
        if(i == itemArray.count-1){
            totalHeight = top+height;
        }
    }
    [self.itemView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(totalHeight));
    }];
    
    CGFloat marginTop = [self.resultArray containsObject:@"其他"]?viewPix(12):0;
    CGFloat textHeight = [self.resultArray containsObject:@"其他"]?viewPix(32):0;
    [self.contentTF mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.itemView.mas_bottom).offset(marginTop);
        make.height.equalTo(@(textHeight));
    }];
    self.contentTF.text = @"";
    for (NSString *content in self.resultArray) {
        if(![titleArray containsObject:content] && [self.resultArray containsObject:@"其他"]){
            self.contentTF.text = content;
            break;
        }
    }
}


-(void)itemButtonTouchAction:(UIButton *)sender{
    if([self.type isEqualToString:@"radio"]){
        //单选
        for (UIButton *button in self.btnArray) {
            button.selected = NO;
        }
        sender.selected = YES;
        NSDictionary *tempDic = self.itemArray[sender.tag-1000];
        if(self.delegate && [self.delegate respondsToSelector:@selector(itemSelectResult:type:)]){
            [self.delegate itemSelectResult:LGNSString(tempDic[@"value"]) type:self.type];
        }
    }else{
        //多选
        sender.selected = !sender.selected;
        NSString *result = @"";
        for (NSInteger i=0; i<self.btnArray.count; i++) {
            UIButton *button = self.btnArray[i];
            if(button.selected == YES){
                NSDictionary *tempDic = self.itemArray[button.tag-1000];
                result = (result.length>0)?[NSString stringWithFormat:@"%@,%@",result,tempDic[@"value"]]:LGNSString(tempDic[@"value"]);
            }
        }
        if(self.contentTF.text.length>0 && [result containsString:@"其他"]){
            result = (result.length>0)?[NSString stringWithFormat:@"%@,%@",result,self.contentTF.text]:self.contentTF.text;
        }
        if(self.delegate && [self.delegate respondsToSelector:@selector(itemSelectResult:type:)]){
            [self.delegate itemSelectResult:result type:self.type];
        }
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    NSString *result = @"";
    for (NSInteger i=0; i<self.btnArray.count; i++) {
        UIButton *button = self.btnArray[i];
        if(button.selected == YES){
            NSDictionary *tempDic = self.itemArray[button.tag-1000];
            result = (result.length>0)?[NSString stringWithFormat:@"%@,%@",result,tempDic[@"value"]]:LGNSString(tempDic[@"value"]);
        }
    }
    if(textField.text.length>0){
        result = (result.length>0)?[NSString stringWithFormat:@"%@,%@",result,textField.text]:textField.text;
    }
    if(self.delegate && [self.delegate respondsToSelector:@selector(itemSelectResult:type:)]){
        [self.delegate itemSelectResult:result type:self.type];
    }
}

-(void)textFieldEditingChanged:(UITextField *)textField{
    NSString *content = textField.text;
    content = (content.length>20)?[content substringToIndex:20]:content;
    self.contentTF.text = content;
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.clipsToBounds = YES;
    self.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.baseView];
    [self.baseView addSubview:self.titleBtn];
    [self.baseView addSubview:self.itemView];
    [self.baseView addSubview:self.contentTF];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.right.equalTo(self.contentView).offset(-viewPix(16));
    }];
    [self.itemView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseView).offset(viewPix(29));
        make.left.equalTo(self.baseView).offset(viewPix(12));
        make.right.equalTo(self.baseView).offset(-viewPix(12));
        make.height.equalTo(@(viewPix(32)));
    }];
    [self.contentTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.itemView.mas_bottom).offset(viewPix(10));
        make.left.equalTo(self.baseView).offset(viewPix(12));
        make.right.equalTo(self.baseView).offset(-viewPix(12));
        make.bottom.equalTo(self.baseView).offset(-viewPix(6));
        make.height.equalTo(@(viewPix(32)));
    }];
}

-(NSMutableArray *)btnArray{
    if(!_btnArray){
        _btnArray = [NSMutableArray array];
    }
    return _btnArray;
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B2BDC7" offset:CGSizeMake(0, 2) opacity:0.15 radius:0];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(UIButton *)titleBtn{
    if(!_titleBtn){
        _titleBtn = [UIButton buttonWithTitle:@"" titleFont:LGFontWeight(15, UIFontWeightMedium) textColor:@"#333333" imageName:@"必选项"];
        _titleBtn.frame = CGRectMake(viewPix(12), viewPix(6), viewPix(200), viewPix(20));
        _titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _titleBtn.userInteractionEnabled = NO;
    }
    return _titleBtn;
}

-(UIView *)itemView{
    if(!_itemView){
        _itemView = [[UIView alloc] init];
    }
    return _itemView;
}

-(UITextField *)contentTF{
    if(!_contentTF){
        _contentTF = [[UITextField alloc]init];
        _contentTF.layer.borderColor = [UIColor colorWithString:@"#E3E3E3"].CGColor;
        _contentTF.layer.borderWidth = 1;
        _contentTF.cornerRidus = 5;
        _contentTF.tintColor = [UIColor colorWithString:@"#333333"];
        _contentTF.textColor = [UIColor colorWithString:@"#333333"];
        _contentTF.textAlignment = NSTextAlignmentLeft;
        _contentTF.keyboardType = UIKeyboardTypeDefault;
        _contentTF.placeholder = @"请填写其他问诊需求";
        _contentTF.font = LGFont(14);
        _contentTF.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewPix(12), viewPix(32))];
        _contentTF.leftViewMode = UITextFieldViewModeAlways;
        _contentTF.delegate = self;
        [_contentTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _contentTF;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
